package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.KeyboardShortcutGroup;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.common.X3CarPoolAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.bean.main.CarModelSelectBean;
import com.das.mechanic_base.bean.main.CarPoolBean;
import com.das.mechanic_base.utils.LanguageConvent;
import com.das.mechanic_base.utils.X3PinYinCarPool;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 车辆池
 */
public class X3CarPoolDailog extends X3BaseBottomSheetDialog implements X3SlideBar.onTouchLetterListener, X3CarPoolAdapter.IOnItemClick {

    private TextView tv_title;
    private RecyclerView rlv_brand;
    private ProgressBar pb_bar;
    private X3SlideBar sb_bar;
    private TextView tv_num;

    private List<CarPoolBean> mList = new ArrayList<>();
    private X3PinYinCarPool pinyinComparator;
    private List<String> headerList;
    private X3CarPoolAdapter carPoolAdapter;
    private RelativeLayout rl_empty;
    private TextView tv_empty;

    public X3CarPoolDailog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_title = getView(R.id.tv_title);
        pb_bar = getView(R.id.pb_bar);
        rlv_brand = getView(R.id.rlv_brand);
        sb_bar = getView(R.id.sb_bar);
        tv_num = getView(R.id.tv_num);
        rl_empty = getView(R.id.rl_empty);
        tv_empty = getView(R.id.tv_empty);


        pinyinComparator = new X3PinYinCarPool();

        sb_bar.setOnTouchLetterListener(this);

        rlv_brand.setLayoutManager(new LinearLayoutManager(mContext));
        rlv_brand.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                LinearLayoutManager layoutManager = (LinearLayoutManager) rlv_brand.getLayoutManager();
                int itemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                if (itemPosition < 0 || itemPosition >= mList.size()) {
                    return;
                }
                String sortLetters = mList.get(itemPosition).sortLetters;
                if (X3StringUtils.isEmpty(sortLetters)) {
                    return;
                }
                for (int i = 0; i < headerList.size(); i++) {
                    if (sortLetters.equalsIgnoreCase(headerList.get(i))) {
                        if (sb_bar != null) {
                            sb_bar.changeSelectIndex(i);
                        }
                        break;
                    }
                }
            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_dialog_carpool;
    }

    public void changeData(String title, List<CarPoolBean> dataLists,List<CarModelSelectBean> selectLists) {
        if(X3StringUtils.isListEmpty(dataLists)){
            rl_empty.setVisibility(View.VISIBLE);
            tv_empty.setText(mContext.getString(R.string.x3_home_no_select));
            pb_bar.setVisibility(View.GONE);
            return;
        }

        rl_empty.setVisibility(View.GONE);
        pb_bar.setVisibility(View.GONE);
        rlv_brand.setVisibility(View.VISIBLE);
        tv_title.setText(title);
        mList.clear();
        mList = LanguageConvent.filledCarPool(dataLists);
        Collections.sort(mList,pinyinComparator);
        headerList = new ArrayList<>();
        for (CarPoolBean carPoolBean : mList) {
            headerList.add(carPoolBean.sortLetters);
        }
        Set<String> set = new LinkedHashSet<>(headerList);
        headerList = new ArrayList<>(set);
        if (sb_bar != null) {
            sb_bar.changeNum(headerList);
        }
        rlv_brand.addItemDecoration(new X3UserItemDecoration());
        carPoolAdapter = new X3CarPoolAdapter(mContext);
        rlv_brand.setAdapter(carPoolAdapter);
        carPoolAdapter.setData(mList);
        carPoolAdapter.setiOnItemClick(this);

    }

    @Override
    public void show() {
        super.show();
        rlv_brand.setVisibility(View.GONE);
        pb_bar.setVisibility(View.VISIBLE);

    }

    @Override
    public void onTouchLetterChange(String letter, boolean touch) {
        if (touch) {
            tv_num.setText(letter);
            tv_num.setVisibility(View.VISIBLE);
        }else {
            tv_num.setVisibility(View.GONE);
        }
        for (int i = 0; i < mList.size(); i++) {
            if (letter.equalsIgnoreCase(mList.get(i).sortLetters)) {
                ((LinearLayoutManager)rlv_brand.getLayoutManager()).scrollToPositionWithOffset(i,0);
                break;
            }
        }
    }

    @Override
    public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, @Nullable Menu menu, int deviceId) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    public void iOnItemClick(CarPoolBean carPoolBean) {
        if(iOnClick != null){
            iOnClick.iOnClickPool(carPoolBean);
        }
    }

    IOnClickCar iOnClick;
    public interface IOnClickCar {
        void iOnClickPool(CarPoolBean brandBean);

    }
    public void setOnItemClick(IOnClickCar iOnClickCar){
        this.iOnClick = iOnClickCar;
    };

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("车辆池弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("车辆池弹窗");
    }
}
